package com.hy.heath.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.heath.common.UserHolder;
import com.hy.heath.mapper.UserMapper;
import com.hy.heath.pojo.entity.User;
import com.hy.heath.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public Boolean login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        wrapper.eq(User::getPassword, password);
        User user1 = getOne(wrapper);
        if (user1 == null) {
            return false;
        }
        UserHolder.saveUser(user1);
        return true;
    }

    @Override
    public Boolean register(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        wrapper.eq(User::getPassword, password);
        User user1 = getOne(wrapper);
        if (user1 != null) {
            return false;
        }
        save(user);
        return true;
    }
}
